using UnityEngine;

public class TableTennisReturns : TableTennisGameTypeBase
{
	private new TableTennisMatchController match;

	private MatchUI ui;

	private new TableTennisPlayerBase servingPlayer;

	private MatchParameters.MatchParams matchParams;

	private ShotParams currentShot;

	protected int ballsAvailable = 5;

	protected int ballsUsed;

	public int maxReturnedBalls { get; protected set; }

	public override void Init(TableTennisMatchController match, MatchUI ui, MatchParameters.MatchParams matchParams)
	{
		this.matchParams = matchParams;
		this.match = match;
		this.ui = ui;
		ballsAvailable = matchParams.ballsAvailable;
		match.CreateHumanPlayer(-1, true);
		match.CreateOpponentPlayer(1, true);
		if (matchParams.seasonEvent != null)
		{
			SeasonDefinitions.SeasonEvent seasonEvent = matchParams.seasonEvent;
			TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
			careerStatsManager.ReportGameStarted(seasonEvent);
			careerStatsManager.AdvanceActiveEvent(seasonEvent, TournamentCareerStats.EventScore.NotRanked, false);
		}
		if (matchParams.eventsDelegate != null)
		{
			matchParams.eventsDelegate.StartMatch(match);
		}
		OnCameraBehaviourDone();
		match.ui.singlePlayerUI.SetActive(false);
		match.ui.hitsDisplay.Show(0, ballsAvailable, ballsAvailable);
		match.ui.hitsDisplay.SetTitle("Returns");
		match.ui.hitsDisplay.ShowCountingLabel(0);
	}

	private void OnCameraBehaviourDone()
	{
		isMatchReady = true;
	}

	public override void StartMatch(int activePlayer = 0)
	{
		servingPlayer = match.GetPlayer(0);
		ui.qualityDisplay.gameObject.SetActive(true);
		ui.cameraChangeButton.SetActive(true);
		OnIntroComplete();
	}

	private new void OnIntroComplete()
	{
		StartServe(true);
	}

	public override void OnMissedServe()
	{
		StartServe(false);
	}

	private void StartServe(bool takeNewBall)
	{
		if (takeNewBall)
		{
			ballsUsed++;
			match.ui.hitsDisplay.UpdateBalls(ballsAvailable - ballsUsed, ballsAvailable);
		}
		base.numShotsInPoint = 0;
		match.ui.hitsDisplay.ShowCountingLabel(base.numShotsInPoint);
		match.StartServe(servingPlayer);
	}

	public override void OnPlayerHitBall(TableTennisPlayerBase player, HitBallParamsPhysicsFlight hitBallParams)
	{
		if (match.ball.isBallInGame || match.ball.isServeBall)
		{
			base.numShotsInPoint++;
			match.ball.isBallInGame = true;
			match.ui.hitsDisplay.UpdateCountingLabel(base.numShotsInPoint);
			maxReturnedBalls = Mathf.Max(base.numShotsInPoint, maxReturnedBalls);
			match.ui.hitsDisplay.UpdateScore(maxReturnedBalls);
			currentShot = new ShotParams();
			currentShot.isServeShot = match.ball.isServeBall;
		}
	}

	public override void OnBallHitObject(PhysicsBall ball, Vector3 pos, Vector3 vel, HitObjectTag hitObjectTag)
	{
		if (!match.ball.isBallInGame)
		{
			return;
		}
		if (match.ball.isServeBall && hitObjectTag == HitObjectTag.Net)
		{
			currentShot.hitNet = true;
			return;
		}
		if (hitObjectTag == HitObjectTag.Table)
		{
			bool flag = Mathf.Sign(pos.z) == (float)match.ball.playerThatHitBall.tableSide;
			bool flag2 = currentShot.isServeShot && currentShot.touchesInTable == 0;
			if (flag != flag2)
			{
				AwardPointTo(match.Other(match.ball.playerThatHitBall), false);
				return;
			}
			currentShot.touchesInTable++;
		}
		int num = ((!currentShot.isServeShot) ? 1 : 2);
		if (currentShot.isServeShot && currentShot.hitNet && currentShot.touchesInTable == num)
		{
			match.ball.isBallInGame = false;
			this.WaitAndExecute(1f, delegate
			{
				StartServe(false);
			});
		}
		else if (currentShot.touchesInTable > num)
		{
			AwardPointTo(match.ball.playerThatHitBall, false);
		}
		else if (hitObjectTag == HitObjectTag.Floor)
		{
			if (currentShot.touchesInTable == num)
			{
				AwardPointTo(match.ball.playerThatHitBall, false);
			}
			else
			{
				AwardPointTo(match.Other(match.ball.playerThatHitBall), false);
			}
		}
	}

	private void AwardPointTo(TableTennisPlayerBase player, bool isTimeOut)
	{
		match.ball.isBallInGame = false;
		AwardPointParams awardPointParams = default(AwardPointParams);
		awardPointParams.playerThatWonPoint = player;
		awardPointParams.isTimeOut = isTimeOut;
		awardPointParams.isDisconnect = false;
		match.AwardPoint(awardPointParams);
	}

	public override void ContinueFromLastPoint(AwardPointParams lastPoint)
	{
		if (ballsUsed >= ballsAvailable)
		{
			OnGameDone();
			return;
		}
		this.WaitAndExecute(1f, delegate
		{
			StartServe(true);
		});
	}

	public void OnGameDone()
	{
		if (matchParams.eventsDelegate != null)
		{
			matchParams.eventsDelegate.MatchComplete(match);
		}
	}
}
